package com.example.trans.controller;

import com.example.trans.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/trans")
@Slf4j
public class TransController {
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 正常运行，事务自动提交
     * 对异常进行捕获，事务会正常提交
     * 不捕获，事务会回滚
     */
    @RequestMapping("/r1")
    @Transactional
    public Boolean r1(String userName, String password){
        log.info("获取到参数userName: {}, password: {}", userName, password);
        Integer result = userInfoService.insertUserInfo(userName, password);
        System.out.println("插入结果, result: " + result);
        try {
            int a = 10 / 0;
        }catch (Exception e){
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 虽然对异常进行捕获，但是捕获后又抛出异常
     * 此时事务回滚
     */
    @RequestMapping("/r2")
    @Transactional
    public Boolean r2(String userName, String password){
        log.info("获取到参数userName: {}, password: {}", userName, password);
        Integer result = userInfoService.insertUserInfo(userName, password);
        System.out.println("插入结果, result: " + result);
        try {
            int a = 10 / 0;
        }catch (Exception e){
            throw e;
        }
        return true;
    }

    /**
     * 事务提交
     */
    @RequestMapping("/r3")
    @Transactional
    public Boolean r3(String userName, String password) throws IOException{
        log.info("获取到参数userName: {}, password: {}", userName, password);
        Integer result = userInfoService.insertUserInfo(userName, password);
        System.out.println("插入结果, result: " + result);
        try {
            int a = 10 / 0;
        }catch (Exception e){
            throw new IOException();
        }
        return true;
    }

    /**
     * 事务回滚
     */
    @RequestMapping("/r4")
    @Transactional
    public Boolean r4(String userName, String password) throws IOException{
        log.info("获取到参数userName: {}, password: {}", userName, password);
        Integer result = userInfoService.insertUserInfo(userName, password);
        System.out.println("插入结果, result: " + result);
        try {
            int a = 10 / 0;
        }catch (Exception e){
            throw new RuntimeException();
        }
        return true;
    }

    /**
     * 事务回滚
     */
    @RequestMapping("/r5")
    @Transactional(rollbackFor = Exception.class)
    public Boolean r5(String userName, String password) throws IOException{
        log.info("获取到参数userName: {}, password: {}", userName, password);
        Integer result = userInfoService.insertUserInfo(userName, password);
        System.out.println("插入结果, result: " + result);
        try {
            int a = 10 / 0;
        }catch (Exception e){
            throw new IOException();
        }
        return true;
    }
}
